<?	
if ($func == 'search')
{
	$tour_search_fields						= array();
	
	$tour_search_fields['tour_type'] 		= $rad_tour_type;
	$tour_search_fields['tour_departure']	= $sbo_tour_departure;
	$tour_search_fields['tour_destination']	= $sbo_tour_destination;
	$tour_search_fields['tour_price']		= $sbo_tour_price;
	$tour_search_fields['tour_start_date']	= $txt_tour_start_date;
	
	$_SESSION['tour_search_fields'] 		= $tour_search_fields;
	
	$qtype 			= "";
	$qdeparture 	= "";
	$qdestination 	= "";
	$qprice			= "";
	$qstart_date	= "";
}

$tours = array();

$selects = array('a.*');
$from = array();
$from['table_name'] = 'tour';
$from['alias'] 		= 'a';
$froms = array();
$froms[] = $from;

$from = array();
$from['table_name'] = 'currency';
$from['alias'] 		= 'cur';

if ($sbo_tour_departure != 0) {
	$from = array();
	$from['table_name'] = 'tour_location';
	$from['alias'] 		= 'b';
	$froms[] = $from;
	// $qdeparture 	= " AND b.location_index = 1 AND b.id = '".$sbo_tour_departure."' ";
	$qdeparture_1 = array('AND', 'b.location_index', '=', 1);
	$qdeparture_2 = array('AND', 'b.location_id', '=', "'".$sbo_tour_departure."'");
	$qdeparture_3 = array('AND', 'b.tour_id', '=', 'a.id');
}

if ($sbo_tour_destination != 0) {
	$from = array();
	$from['table_name'] = 'tour_location';
	$from['alias'] 		= 'c';
	$froms[] = $from;
	// $qdestination 	= " AND b.id = '".$sbo_tour_destination."' AND b.location_index = (SELECT max(location_index) FROM tour_location WHERE tour_location.tour_id = a.id) ";
	$qdestination_1 = array('AND', 'c.location_id', '=', "'".$sbo_tour_destination."'");
	$qdestination_2 = array('AND', 'c.location_index', '=', '(SELECT max(location_index) FROM tour_location WHERE tour_location.tour_id = a.id)');
	$qdestination_3 = array('AND', 'c.tour_id', '=', 'a.id');
}

if ($sbo_tour_price != 0) {
	$price = new PriceSection();
	$price->select($sbo_tour_price);
	//$qprice 		= " AND a.price >= 500000 AND a.price <= 1000000 ";
	$qprice_1 = array('AND', 'a.price', '>=', "(".$price->getmin_value()."*cur.Sell)");
	$qprice_2 = array('AND', 'a.price', '<=', "(".$price->getmax_value()."*cur.Sell)");
}

if ($txt_tour_start_date != "")
{
	$txt_tour_start_date = lg_date::dmy2ymd($txt_tour_start_date);
	//$qstart_date 	= " AND a.start_date = '".$txt_tour_start_date."' ";
	$qstart_date = array('AND', 'a.start_date', '=', "'".$txt_tour_start_date."'");	
}

$where_clause = array(array(null, 'a.active', '=', 1), $qtype, $qprice_1, $qprice_2, $qstart_date, $qdeparture_1, $qdeparture_2, $qdeparture_3, $qdestination_1, $qdestination_2, $qdestination_3);

// COUNT total
$tours_temp = Database::doQuery($selects, $froms, null, $where_clause);

$page		=	$page + 0;
$perpage	=	10;
$sum		=	sizeof($tours_temp);
$pages		=	($sum-($sum%$perpage))/$perpage;
if ($sum % $perpage <> 0 )	$pages = $pages+1;
$page		=	($page==0)?1:(($page>$pages)?$pages:$page);
$min 		= 	abs($page-1) * $perpage;
$max 		= 	$perpage;

// To search tour.
$tours_temp		= 	Database::doQuery($selects, $froms, null, $where_clause, null, null, array($min,$max));

foreach ($tours_temp as $tour_temp)
{
	$tour = new Tour();
	$tour->select($tour_temp['id']);
	$tours[] = $tour;
}

?>
<div class='tour_left'>
	<div class='title_1 cufon' style='width:595px;'>
        Kết quả tìm kiếm
    </div>
	<div class='wrap_tour_content'>
    	<div class='search_content'>
            <? include('z_includes/tour_search.php'); ?>
        </div>
	    <? include('z_includes/tour_filter.php'); ?>
		<div class='tour_list' id='load_tour'>
        	<? include('z_includes/tour_list.php'); ?>
        </div>
    </div>
</div>

<div class='tour_right'>
<?
include('z_includes/tour_right.php');
?>
</div>
<div style='clear:both'></div>